// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({env: 'cloud1-5glocnmp62950062'})
const db = cloud.database()

// 云函数入口函数
exports.main = async (event, context) => {
  let dynamicid = event.dynamicid
  const wxContext = cloud.getWXContext()
  let result = db.collection('approvalList').where(
    {
      dynamicid: dynamicid,
      openid: wxContext.OPENID
    }).get()
    if((await result).data.length==0){
      db.collection('approvalList').add({
        data:{
          dynamicid: dynamicid,
          openid: wxContext.OPENID,
          status: true
        }
      })
    }else{
      let status= (await result).data[0].status
      let id = (await result).data[0]._id
      db.collection('approvalList').doc(id).update({
        data:{
          status: !status
        }
      })
    }

  return {
    result
  }
}